package com.sambo.inspection.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonElement;
import com.sambo.inspection.R;
import com.sambo.inspection.activity.support.BaseActivity;
import com.sambo.inspection.adapter.FullyGridLayoutManager;
import com.sambo.inspection.adapter.MediaListAdapter;
import com.sambo.inspection.bean.MediaFileBean;
import com.sambo.inspection.bean.OrderFileBean;
import com.sambo.inspection.bean.WorkReportBean;
import com.sambo.inspection.http.HttpClient;
import com.sambo.inspection.http.HttpResponseHandler;
import com.sambo.inspection.http.Urls;
import com.sambo.inspection.util.JsonUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WorkOrderProcessedDetailActivity extends BaseActivity implements View.OnClickListener {

    ImageView ivBack;
    TextView tvCheckNumber;
    TextView tvDivisional;
    TextView tvEquipmentType;
    TextView tvTitle;
    TextView tvReportTime;
    TextView tvContent;
    RecyclerView rvEventReportRes;
    RecyclerView rvEventHandleRes;
    TextView tvTreatmentMethods;
    TextView tvHandleTime;
    TextView tvWebTitle;
    LinearLayout llShowHidden;

    private MediaListAdapter reportMediaListAdapter;
    private MediaListAdapter handleMediaListAdapter;

    private List<MediaFileBean> reportMediaDataList = new ArrayList<>();
    private List<MediaFileBean> handleMediaDataList = new ArrayList<>();

    private WorkReportBean mWorkReportBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_order_processed_detail);
        ivBack = findViewById(R.id.iv_back);
        tvCheckNumber = findViewById(R.id.tv_check_number);
        tvDivisional = findViewById(R.id.tv_divisional);
        tvEquipmentType = findViewById(R.id.tv_equipment_type);
        tvTitle = findViewById(R.id.tv_title);
        tvReportTime = findViewById(R.id.tv_report_time);
        tvContent = findViewById(R.id.tv_content);
        tvTreatmentMethods = findViewById(R.id.tv_treatment_methods);
        tvHandleTime = findViewById(R.id.tv_handle_time);
        tvWebTitle = findViewById(R.id.tv_web_title);
        llShowHidden = findViewById(R.id.ll_show_hidden);
        rvEventReportRes = findViewById(R.id.rv_event_report_res);
        rvEventHandleRes = findViewById(R.id.rv_event_handle_res);

        String title = getIntent().getStringExtra("title");
        tvWebTitle.setText(title);

        mWorkReportBean = (WorkReportBean) getIntent().getSerializableExtra("model");
        if (mWorkReportBean != null) {
            tvCheckNumber.setText(mWorkReportBean.getCheckPointCode());
//            tvDivisional.setText(bean.getBelongRegion());
//            tvEquipmentType.setText(bean.getEqType());
            tvTitle.setText(mWorkReportBean.getTitle());
            tvReportTime.setText(mWorkReportBean.getCreatedTime());
            tvContent.setText(mWorkReportBean.getContent());

            if (TextUtils.equals("2", mWorkReportBean.getState())
                    || TextUtils.equals("3", mWorkReportBean.getState())) {
                llShowHidden.setVisibility(View.GONE);
            } else if (TextUtils.equals("4", mWorkReportBean.getState())
                    || TextUtils.equals("5", mWorkReportBean.getState())) {
                tvTreatmentMethods.setText(mWorkReportBean.getDealContent());
                tvHandleTime.setText(mWorkReportBean.getEndDate());
                llShowHidden.setVisibility(View.VISIBLE);
            }
        }

        FullyGridLayoutManager reportManager = new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
        FullyGridLayoutManager handleManager = new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
        rvEventReportRes.setLayoutManager(reportManager);
        rvEventHandleRes.setLayoutManager(handleManager);

        reportMediaListAdapter = new MediaListAdapter(reportMediaDataList);
        handleMediaListAdapter = new MediaListAdapter(handleMediaDataList);

        rvEventReportRes.setAdapter(reportMediaListAdapter);
        rvEventHandleRes.setAdapter(handleMediaListAdapter);

        ivBack.setOnClickListener(this);

        getMediaFileInfo();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    private void getMediaFileInfo() {

        if (mWorkReportBean == null) {
            return;
        }

        Map<String, String> params = new HashMap<>();
        params.put("orderid", String.valueOf(mWorkReportBean.getId()));

        HttpClient.get(this, Urls.API_SERVER_HOST + Urls.GET_ORDER_FILES, params,
                new HttpResponseHandler() {
                    @Override
                    public void onInnovationSuccess(JsonElement value) {
                        super.onInnovationSuccess(value);
                        OrderFileBean[] response = JsonUtil.toObj(value.toString(), OrderFileBean[].class);

                        reportMediaDataList.clear();
                        handleMediaDataList.clear();

                        if (response != null && response.length > 0) {

                            MediaFileBean bean;

                            for (OrderFileBean orderFileBean : response) {
                                bean = new MediaFileBean();
                                bean.setId(orderFileBean.getId());
                                bean.setUrl(orderFileBean.getUrl());
                                bean.setType(2);
                                bean.setBusinessType(orderFileBean.getBusinessType());
                                if (TextUtils.equals("0", orderFileBean.getFileType())) {
                                    bean.setMediaType(1);
                                } else {
                                    bean.setMediaType(2);
                                }

                                if (TextUtils.equals("0", orderFileBean.getBusinessType())) {
                                    reportMediaDataList.add(bean);
                                } else {
                                    handleMediaDataList.add(bean);
                                }
                            }
                        }

                        reportMediaListAdapter.notifyDataSetChanged();
                        handleMediaListAdapter.notifyDataSetChanged();
                    }

                }, new HttpClient.NetworkUnavailableListener() {
                    @Override
                    public void networkUnavailable() {
                        Toast.makeText(WorkOrderProcessedDetailActivity.this, "网络连接异常", Toast.LENGTH_SHORT).show();
                    }
                });
    }
}
